package com.tanovo.service.impl;

import com.tanovo.bo.UserCore;
import com.tanovo.domain.Resources;
import com.tanovo.domain.Role;
import com.tanovo.domain.User;
import com.tanovo.mapper.ResourcesMapper;
import com.tanovo.mapper.RoleMapper;
import com.tanovo.mapper.UserMapper;
import com.tanovo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ResourcesMapper resourcesMapper;

    @Override
    public Boolean checkUserResouce(UserCore userCore) {
        //首先获取角色
        List<Role> roleList = roleMapper.getRoleListByUserId(userCore);
        //获取角色对应的资源
        for (Role role : roleList) {
            List<Resources> resourcesList = resourcesMapper.getResouesByRole(role);
            AntPathMatcher pathMatcher = new AntPathMatcher();
            for (Resources resource : resourcesList) {
                boolean match = pathMatcher.match(resource.getPath().trim(), userCore.getRequestURI());
                if (match&&resource.getEnable()==1) {
                    return true;
                }
            }

        }
        return false;
    }

    @Override
    public User getUserById(String userId) {
        return userMapper.selectByPrimaryKey(new Integer(userId));
    }
}
